import Toast from "@/miniprogram_npm/@vant/weapp/toast/toast";

// pages/base/device/record/record.ts
Page<
  {},
  {
    data: {
      tempFilePath: null | string;
    };
    recorderManager?: WechatMiniprogram.RecorderManager;
    /**
     * 开始录音
     */
    onStart: () => void;
    /**
     * 停止录音
     */
    onStop: () => void;
    /**
     * 播放录音
     */
    onPlay: () => void;
    /**
     * 跳转地址
     * @param e
     * @returns
     */
    onToDocDetail: (e: WechatMiniprogram.BaseEvent) => void;
  }
>({
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 录音文件地址
     */
    tempFilePath: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.onStart(() => {
      Toast("开始录音");
      console.log("recorder start");
    });
    this.recorderManager.onPause(() => {
      Toast("暂停录音");
      console.log("recorder pause");
    });
    const that = this;
    this.recorderManager.onStop((res) => {
      Toast("停止录音");
      console.log("recorder stop", res);
      const { tempFilePath } = res;
      console.log("临时文件地址:", tempFilePath);
      that.data.tempFilePath = tempFilePath;
    });
    this.recorderManager.onFrameRecorded((res) => {
      const { frameBuffer } = res;
      console.log("frameBuffer.byteLength", frameBuffer.byteLength);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  onStart() {
    // 配置详情：https://developers.weixin.qq.com/miniprogram/dev/api/media/recorder/RecorderManager.start.html
    const options: WechatMiniprogram.RecorderManagerStartOption = {
      duration: 10000,
      sampleRate: 44100,
      numberOfChannels: 1,
      encodeBitRate: 192000,
      format: "aac",
      frameSize: 50,
    };
    if (this.recorderManager) {
      this.recorderManager.start(options);
    }
  },

  onStop() {
    if (this.recorderManager) {
      this.recorderManager.stop();
    }
  },

  onPlay() {
    if (this.data.tempFilePath) {
      const innerAudioContext = wx.createInnerAudioContext();
      // 是否自动播放
      innerAudioContext.autoplay = true;
      // 设置音频文件的路径
      innerAudioContext.src = this.data.tempFilePath;
      // 播放音频文件
      innerAudioContext.onPlay(() => {
        console.log("开始播放");
      });
      innerAudioContext.onEnded(() => {
        Toast("录音播放完毕");
      });
      // 监听音频播放错误事件
      innerAudioContext.onError((res) => {
        console.log(res.errMsg);
        console.log(res.errCode);
      });
    } else {
      Toast("不存在录音文件");
    }
  },
  onToDocDetail(e: WechatMiniprogram.BaseEvent) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url:
        "/pages/base/wrapper-web-view/wrapper-web-view?url=" +
        encodeURIComponent(url),
    });
  },
});
